package com.wuzuqing.android.eightdiagrams.term;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description:
 * @Author: 吴祖清
 * @CreateDate: 29.1.24 5:06 PM
 */
public class ExactDate {
    private int year;
    private int month;
    private int date;
    private int hour;
    private int minute;
    private int second;
    private int milliseconds;
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final SimpleDateFormat sSdf = new SimpleDateFormat(yyyyMMddHHmmss);

    public ExactDate(int year, int month, int date, int hour, int minute, int second) {
        this.year = year;
        this.month = month;
        this.date = date;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        offset = OFFSET;
    }


    public static ExactDate fromYmd(int y, int m, int d) {
        return fromYmdHms(y, m, d, 0, 0, 0);
    }

    public static ExactDate fromCal(Calendar calendar) {
        return fromYmdHms(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1,
                calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR_OF_DAY
                ), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
    }

    public static ExactDate fromTimeInMillis(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return fromYmdHms(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1,
                calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR_OF_DAY
                ), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
    }

    public static ExactDate fromYmdHms(int y, int m, int d, int hour, int minute, int second) {
//        Date date = new Date(String.format("%d/%d/%d %d:%d:%d GMT+0800", y, m, d, hour, minute, second));
        return new ExactDate(y, m, d, hour, minute, second);
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        Calendar cal = getCal();
        cal.add(Calendar.YEAR, year - this.year);
        resetData(cal);
    }

    public void setFullYear(int year){
        Calendar cal = getCal();
        cal.set(Calendar.YEAR,year);
        resetData(cal);
    }

    public int getMonth() {
        return month;
    }

    private Calendar getCal() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date(year - 1900, month - 1, this.date, hour, minute, second);
        calendar.setTime(date);
        return calendar;
    }

    public void setMonth(int month) {
        Calendar cal = getCal();
        int maximum = cal.getMaximum(Calendar.MONTH);
        if (month > maximum) {
            cal.set(Calendar.MONTH, 0);
            cal.add(Calendar.MONTH, month);
        } else {
            cal.set(Calendar.MONTH, month);
        }
        resetData(cal);
    }

    private void resetData(Calendar calendar) {
        this.year = calendar.get(Calendar.YEAR);
        this.month = calendar.get(Calendar.MONTH) + 1;
        this.date = calendar.get(Calendar.DATE);
        this.hour = calendar.get(Calendar.HOUR_OF_DAY);
        this.minute = calendar.get(Calendar.MINUTE);
        this.second = calendar.get(Calendar.SECOND);
        this.milliseconds = calendar.get(Calendar.MILLISECOND);
//        System.out.println(String.format("%02d:%02d:%02d",hour,minute,second));
    }

    public int getDate() {
        return date;
    }

    public int getDay() {
        return date;
    }

    public int getWeek(){
      return   getCal().get(Calendar.DAY_OF_WEEK)-1;
    }

    public void setDate(int date) {
        Calendar cal = getCal();
        int maximum = cal.getMaximum(Calendar.DATE);
        if (date > maximum) {
            cal.set(Calendar.DATE, 1);
            cal.add(Calendar.DATE, date-1);
        } else {
            cal.set(Calendar.DATE, date);
        }
        resetData(cal);
    }

    public int getHour() {
        return hour;
    }

    public int getHours() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public void setHours(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public int getMinutes() {
        return minute;
    }

    public int getFullYear() {
        return year;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public void setMinutes(int minute) {
        this.minute = minute;
    }

    public int getSecond() {
        return second;
    }

    public int getSeconds() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public void setMilliseconds(int milliseconds) {
        this.milliseconds = second;
    }

    public void setSeconds(int second) {
        this.second = second;
    }

    private static final long LJD = -2208988458000L;
//    private static final long OFFSET = 0;
    private static final long OFFSET = 343000;
    private long offset = OFFSET;

    public long getTime() {
        Calendar calendar = getCal();
        //343000
        //-2208988458000
        long timeInMillis = calendar.getTimeInMillis();
        if (timeInMillis <= LJD) {
            return timeInMillis;
        }
        return timeInMillis + offset;
    }

    @Override
    public String toString() {
        return year +
                "-" + month +
                "-" + date +
                " " + hour +
                ":" + minute +
                ":" + second +
                " " + milliseconds;
    }

    public void setTime(int hour, int minute, int second, int milliseconds) {
        Calendar cal = getCal();
        offset = 0;
        cal.set(year,month-1,date,hour,minute,second);
        cal.set(Calendar.MILLISECOND, milliseconds);
        resetData(cal);
    }

}
